//
//  BIDEditNickNameViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDEditNickNameViewController.h"
#import "BIDCustomTextField.h"

static NSString *editNickNameURL = @"memberAndroid/modify.action";

@interface BIDEditNickNameViewController ()

@end

@implementation BIDEditNickNameViewController
@synthesize content;
@synthesize delegate;
@synthesize indexPath;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"修改昵称";
    _tf.text = content;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveHandler)];
    _tf.inputAccessoryView = self.toolBar;
    [self.view setBackgroundColor:RGBACOLOR(226, 233, 243, 1.f)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)saveHandler
{
    if(_tf.text.length == 0)
    {
        [self showAlertView:@"昵称不能为空" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    if([_tf.text isEqualToString:content])
    {
        [self showAlertView:@"昵称并没有变化，不需要保存" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        return;
    }
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], editNickNameURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"memberId=%@&memberDetail.nickName=%@", [AppDelegate getUserId], _tf.text];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self showAlertView:@"保存成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler"];
        }
    }];
}

- (void)alertViewHandler
{
    if([_tf.text isEqualToString:content])
    {
    }
    else
    {
        [delegate editNickName:_tf.text atIndexPath:indexPath];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
